package debugServer;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.ConcurrentLinkedQueue;

import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.ScrollPaneConstants;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import debugServer.GlobalModPanel.ActivateListener;
import debugServer.GlobalModPanel.AddListener;
import debugServer.GlobalModPanel.ListListener;

import YAPserver.ClientHandler;
import YAPserver.Lobby;
import YAPserver.logic.Game;

public class LocalModPanel extends JPanel {
	LocalModCode selection;
	ClientHandler player;
	Lobby lobby;
	
	JButton addButton = new JButton("add");
	JButton activateButton = new JButton("activate");
	JList<LocalModCode> modList = new JList<LocalModCode>(LocalModCode.values());
	JList<ClientHandler> playerList;
	JScrollPane modPane = new JScrollPane(modList);
	JScrollPane playerPane;
	JPanel buttonPanel = new JPanel(new BorderLayout());
	
	LocalModPanel(Lobby lobby){
		this.lobby = lobby;
		createPlayerList();
		createPanel();
	}
	
	private void createPlayerList() {
		ClientHandler[] clients = new ClientHandler[lobby.getPlayers().size()];
		int i=0;
		lobby.getPlayers().toArray(clients);
		playerList = new JList<ClientHandler>(clients);
		playerPane = new JScrollPane(playerList);
	}

	private void createPanel(){
		this.setLayout(new GridLayout(1, 4));
		
		addButton.addActionListener(new AddListener());
		activateButton.addActionListener(new ActivateListener());
		buttonPanel.setLayout(new GridLayout(2,1));
		buttonPanel.add(addButton);
		buttonPanel.add(activateButton);
		
		modPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
		modPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		modList.setVisibleRowCount(4);
		modList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		modList.setSelectedIndex(0);
		modList.addListSelectionListener(new ModListListener());
		
		playerPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
		playerPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		playerList.setVisibleRowCount(4);
		playerList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		playerList.setSelectedIndex(0);
		playerList.addListSelectionListener(new PlayerListListener());
		
		this.add(modPane);
		this.add(new JPanel());
		this.add(playerPane);
		this.add(buttonPanel);
		
		selection = modList.getSelectedValue();
		player = playerList.getSelectedValue();
	}
	
	class ActivateListener implements ActionListener{
		
		@Override
		public void actionPerformed(ActionEvent e) {
//			for(ClientHandler client: lobby.getPlayers()){
//				if(client.getName().equals(player)){
//					DebugConsole.activateMod(selection, lobby.getGame(), client.getGameId());
//				}
//			}
			DebugConsole.activateMod(selection, lobby.getGame(), player.getGameId());
		}
		
	}
	
	class AddListener implements ActionListener{
		
		@Override
		public void actionPerformed(ActionEvent e) {
//			for(ClientHandler client: lobby.getPlayers()){
//				if(client.getName().equals(player)){
//					DebugConsole.addMod(selection, lobby.getGame(), client.getGameId());
//				}
//			}	
			DebugConsole.addMod(selection, lobby.getGame(), player.getGameId());
		}
		
	}
	
	class ModListListener implements ListSelectionListener{

		@Override
		public void valueChanged(ListSelectionEvent e) {
			selection = modList.getSelectedValue();
		}
		
	}
	
	class PlayerListListener implements ListSelectionListener{
		@Override
		public void valueChanged(ListSelectionEvent e) {
			player = playerList.getSelectedValue();
		}
	}
}
